package com.ge.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCQuick {

    public static void main(String[] args) throws Exception {

        // 1. 注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        // 2. 获取连接对象
        String url = "jdbc:mysql://localhost:3306/jdbc";
        String username = "root";
        String password = "123456";
        Connection connection = DriverManager.getConnection(url, username, password);

        // 3. 获取执行SQL语句的对象
        Statement statement = connection.createStatement();

        // 4. 编写SQL语句，执行并接受返回的结果集
        String sql = "SELECT id, name, passwd FROM USER";
        ResultSet resultSet = statement.executeQuery(sql);

        // 5. 处理查询结果
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String passwd = resultSet.getString("passwd");
            System.out.println("id: " + id + "\tname: " + name + "\tpasswd: " + passwd);
        }

        // 6. 释放资源（先开后关）
        resultSet.close();
        statement.close();
        connection.close();
    }
}
